package listbyorder.access001_100.test6;

import java.util.ArrayList;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/6 14:26
 */
public class Solution1 {

    // 方法一： 模拟数组遍历轨迹，创建list存储路径
    // 有额外的空间复杂度
    public String convert(String s, int numRows) {
        int len = s.length();
        if (len <= numRows || numRows < 2) return s;
        List<StringBuilder> list = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            list.add(new StringBuilder());
        }
        int index = 0;
        boolean flag = true;
        int cur_hang = 0;  // 当前行数
        while (index < len) {
            if (flag) {
                list.get(cur_hang++).append(s.charAt(index++));
            } else {
                list.get(cur_hang--).append(s.charAt(index++));
            }
            if (cur_hang == numRows - 1) {
                flag = false;
            }
            if (cur_hang == 0) {
                flag = true;
            }
        }
        String res = "";
        for (StringBuilder stringBuilder : list) {
            res += stringBuilder.toString();
        }
        return res;
    }
}
